package com.dawnling.mpchartdemo.ui.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dawnling.mpchartdemo.R;

public class ViewPageActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private TextView textIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_page);

        mViewPager = findViewById(R.id.viewpager);
        textIndex = findViewById(R.id.textIndex);
        // 设置viewpager保留界面不重新加载的页面数量
        mViewPager.setOffscreenPageLimit(2);
        FragmentManager fragmentManager = getSupportFragmentManager();
        mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
            @Override
            public Fragment getItem(int position) {
                return ChartFragment.newInstance(position);
            }
            @Override
            public int getCount() {
                return 4;
            }
        });
        //viewPager滑动监听
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                textIndex.setText((position + 1) + "/4");
            }

            @Override
            public void onPageSelected(int position) {}

            @Override
            public void onPageScrollStateChanged(int state) {}
        });
    }
}
